import { login,logout,refreshToken } from '@/api/auth';
import {PcCookie,Key} from '@/utils/cookie';

// 定义状态
const state = {
  /*userInfo: PcCookie.get(Key.userInfoKey)  ? JSON.parse(PcCookie.get(Key.userInfoKey)) : null, // 用户信息对象*/
  /*userInfo: PcCookie.get(Key.userInfoKey)  ? PcCookie.get(Key.userInfoKey) : null, // 用户信息对象*/
  userInfo: PcCookie.get(Key.userInfoKey)  ? JSON.stringify(PcCookie.get(Key.userInfoKey)): null, // 用户信息对象
  accessToken: PcCookie.get(Key.accessTokenKey), // 访问令牌字符串
  refreshToken: PcCookie.get(Key.refreshTokenKey), // 刷新令牌字符串
}

const mutations = {
  // 赋值用户状态
  SET_USER_STATE(state,data){
    // 解构对象属性
    const {userInfo,access_token,refresh_token} = data;

    // 状态赋值
    state.userInfo = JSON.stringify(userInfo);
    state.accessToken = access_token;
    state.refreshToken = refresh_token;

    // 将数据保存到cookie
    PcCookie.set(Key.userInfoKey,JSON.stringify(userInfo));
    PcCookie.set(Key.accessTokenKey,access_token);
    PcCookie.set(Key.refreshTokenKey,refresh_token);

  },

  // 重置用户状态
  RESET_USER_STATE(state){
    state.userInfo = null;
    state.accessToken = null;
    state.refreshToken = null;

    // 移除cookie中的值
    PcCookie.remove(Key.userInfoKey);
    PcCookie.remove(Key.accessTokenKey);
    PcCookie.remove(Key.refreshTokenKey);
  }
}

// 定义行为
const actions = {
  UserLogin({commit},userData){
    const {username,password} = userData;
    return new Promise((resolve,reject) => {
      login({username:username.trim(),password:password}).then(response => {
        //console.log('resp',response)
        const {code,data} = response;
        console.log('userInfo',JSON.stringify(data));
        if(code === 200){
          // 状态赋值
          commit('SET_USER_STATE',data);
        }
        resolve(response);
      }).catch(err =>{
        // 重置状态
        commit('RESET_USER_STATE');
        reject(err);
      })
    })
  },

  // 退出
  UserLogout({state,commit}){
    console.log("state.accessToken="+state.accessToken)
    logout(state.accessToken).then(response => {
      // console.log(response);
      commit('RESET_USER_STATE');
      /*const {code} = response.data;
      console.log("code=="+code)
      if (code === 200) {

      } else {
        // this.$message.error('系统繁忙,请稍后再试');
      }*/
      // window.location.href = redirectURL || '/';
      // this.$router.replace({path: '/'})
    }).catch(err => {
      // console.log(err);
      // this.$message.error('系统繁忙,请稍后再试');
      // window.location.href = redirectURL || '/'
    })
  },

  // 刷新令牌
  SendRefreshToken({state,commit}){
    return new Promise((resolve,reject) =>{
      // 判断是否有刷新令牌
      if(!state.refreshToken){
        commit('RESET_USER_STATE');
        reject('没有刷新令牌');
        return
      }

      // 发送请求
      refreshToken(state.refreshToken).then(response =>{
        console.log('获取到的新的令牌:',response.data);
        commit('SET_USER_STATE',response.data);
        resolve()
      }).catch(err => {
        commit('RESET_USER_STATE');
        reject(err);
      })
    })
  }
}

export default {
    state,
    mutations,
    actions
}


